package com.controller.home;

import com.controller.BaseController;
import com.entity.VipUser;
import com.service.imple.home.VipUserServiceImple;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**登陆注册页**/
@Controller
@RequestMapping("home/vipUser")
public class VipUserController extends BaseController<VipUserServiceImple> {

    @RequestMapping("/toRegisterVip")
    public String toRegisterVip(){
        return "homepage/register";
    }
    //注册
    @RequestMapping(method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> registerVip(String txtUserName, String txtPassword, String txtMobile, String txtEmail){
        VipUser vipUser = new VipUser();
        vipUser.setAddTime(new Date());
        vipUser.setEmail(txtEmail);
        vipUser.setPassword(txtPassword);
        vipUser.setUsername(txtUserName);
        vipUser.setPhone(txtMobile);
        vipUser.setAddTime(new Date());
        Map<String,Object> map = new HashMap<>();
        int count = BaseService.insert(vipUser);
        if (count>=1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "homepage/login";
    }


    //登录的方法
    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> login(String username, String password, HttpServletRequest httpServletRequest) {

        Map<String, Object> resultJson=new HashMap<String, Object>();
        //当前用户
        Subject currentUser= SecurityUtils.getSubject();
        //验证信息
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken(username,password);
        //登录
        try {
            currentUser.login(usernamePasswordToken);
            if(currentUser.isAuthenticated()) { //表示是否认证成功！
                resultJson.put("status",1);
                resultJson.put("msg","登录成功！");
            }else {
                resultJson.put("status", 0);
                resultJson.put("msg","用户名或密码不正确！");
            }
        }catch (AuthenticationException e) {
            resultJson.put("status", 0);
            resultJson.put("msg","用户名或密码不正确！");
        }
        return resultJson;
    }

    @RequestMapping("toRecharge")
    public String recharge(@SessionAttribute("userId") Integer userId, ModelMap modelMap){
        Map<String,Object> map = new HashMap<>();
        map.put("id",userId);
        List<VipUser> list = BaseService.select(map);
        modelMap.put("list",list);
        return"center/amount/recharge";
    }

    @RequestMapping("UpdateRechargeMoney")
    @ResponseBody
    public Map<String, Object> UpdateRechargeMoney(@SessionAttribute("userId") Integer userId,Integer RechargeMoney){
        Map<String,Object> map = new HashMap<>();
        map.put("id",userId);
        List<VipUser> list= BaseService.select(map);
        list.get(0).setRechargeMoney(BigDecimal.valueOf(RechargeMoney));
        int count = BaseService.update(list.get(0));
        if (count>=1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    @RequestMapping("toProinfo")
    public String toProinfo(@SessionAttribute("userId") Integer userId,ModelMap modelMap){
        VipUser vipUser = new VipUser();
        vipUser.setId(userId);
        List<VipUser> list = BaseService.selectAll(vipUser);
        modelMap.put("list",list);
        return "center/user/proinfo";
    }

    @RequestMapping("Proinfo")
    @ResponseBody
    public Map<String,Object> Proinfo(@SessionAttribute("userId") Integer userId,VipUser vipUser){
        Map<String,Object> map = new HashMap<>();
        map.put("id",vipUser.getId());
        List<VipUser> list = BaseService.select(map);
        list.get(0).setUsername(vipUser.getUsername());
        list.get(0).setUserImg(vipUser.getUserImg());
        list.get(0).setEmail(vipUser.getEmail());
        list.get(0).setPhone(vipUser.getPhone());
        list.get(0).setId(userId);
        int count = BaseService.updateByPrimaryKeyWithBLOBs(list.get(0));
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    @RequestMapping("index")
    public String index(@SessionAttribute("userId")Integer userId, ModelMap modelMap, HttpServletRequest request){
        VipUser vipUser = new VipUser();
        vipUser.setId(userId);
        List<VipUser> list = BaseService.selectAll(vipUser);
        String userImg = list.get(0).getUserImg();
        String username = list.get(0).getUsername();
        request.getSession().setAttribute("userImg",userImg);
        request.getSession().setAttribute("username",username);
        modelMap.put("list",list);
        return "center/index";
    }

    @RequestMapping("ToUpdatePassword")
    public String ToUpdatePassword (@SessionAttribute("userId")Integer userId,ModelMap modelMap){
        VipUser vipUser = new VipUser();
        vipUser.setId(userId);
        List<VipUser> list = BaseService.selectAll(vipUser);
        modelMap.put("list",list.get(0));
        return "center/user/password";
    }

    @RequestMapping("UpdatePassword")
    @ResponseBody
    public Map<String,Object> UpdatePassword (@SessionAttribute("userId")Integer userId,String password){
        Map<String,Object> map = new HashMap<>();
        VipUser vipUser = new VipUser();
        vipUser.setId(userId);
        List<VipUser> list = BaseService.selectAll(vipUser);
        list.get(0).setPassword(password);
        int count =BaseService.updateByPrimaryKeyWithBLOBs(list.get(0));
        if (count==1){
            map.put("status",1);
        }else {
            map.put("status",2);
        }
        return map;
    }

    /**
     * 注销
     */
    @RequestMapping("write")
    public String write(HttpSession session){
        session.invalidate();
        return "homepage/login";
    }




}
